import httpx
import data
import random


def build_head():
    head = {
        "accept": "application/json, text/plain, */*",
        "content-type": "application/x-www-form-urlencoded",
        "origin": "https://alywechat.anlaiye.com",
        "accept-language": "zh-CN,zh-Hans;q=0.9",
        "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.18(0x1800123f) NetType/WIFI Language/zh_CN"
                      "accept-encoding" "gzip, deflate, br"
    }
    return head


def build_shops_param():
    return {
        "target": "merchants",
        "app_version": "8.1.5",
        "action": "/pub/shop/listForGoods",
        "token": "",
        "login_token": "",
        "client_type": "null",
        "school_id": 333,
        "page": 1,
        "pageSize": 20,
    }


def get_shops_status():
    url = "https://web-agent.anlaiye.com/agent/get"
    head_data = build_head()
    param = build_shops_param()
    try:
        response = httpx.get(url, headers=head_data, params=param)
    except httpx.ReadTimeout:
        return []
    if response.status_code == 200:
        resp_data = response.json()
        # opens = [random.randint(1, 5), random.randint(6, 15)]
        # print("警告！调试模式已经打开"）
        # for idx, each in enumerate(resp_data['data']):
        #     if idx in opens:
        #         resp_data['data'][idx]['is_open'] = 1
        return resp_data['data']  # "0"是关闭
    elif response.status_code >= 500:
        return []
    else:
        raise ValueError("检查商店信息失败!")


if __name__ == '__main__':
    d = get_shops_status()
    for each in d:
        print(each)
